//
//  MineViewController.m
//  Journey
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineViewController.h"
#import "LeftViewController.h"
#import <DKNightVersion/DKNightVersion.h>
@interface MineViewController ()

@property(nonatomic,strong)LeftViewController *leftVC;

//设置一个菜单按钮
@property(nonatomic,assign)BOOL isDisplyMenu;

@property(nonatomic,strong)UIImageView *image;
@property(nonatomic,strong)UIButton *button;

@property(nonatomic,strong)UIButton *loginButton;
@property(nonatomic,strong)UIButton *imageButton;
@end

@implementation MineViewController

//- (void)viewWillAppear:(BOOL)animated
//{
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"loginStatus"]isEqualToString:@"1"]) {
//        
//    //    NSLog(@"111");
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    NSString *userName = [defaults objectForKey:@"userName"];
//    NSString *url = [defaults objectForKey:@"headUrl"];
//        NSLog(@"%@",userName);
//        NSLog(@"%@",url);
//     [self.imageButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]] forState:UIControlStateNormal];
//        [self.loginButton setTitle:userName forState:UIControlStateNormal];
//
//    }else{
//        
//        [self.loginButton setTitle:@"立即登录" forState:UIControlStateNormal];
//
//
//    }
//
//}


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden = YES;
    
    //背景图
    self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine"]];
    self.image.frame = self.view.bounds;
    self.image.userInteractionEnabled = YES;
    [self.view addSubview:self.image];
    
    
    //设置
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setImage:[UIImage imageNamed:@"list"] forState:UIControlStateNormal];
    self.button.frame = CGRectMake( 10 * WIDTHC, 10 * WIDTHC, 60 *WIDTHC , 60 *WIDTHC);
    
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(listButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    //setVC用作抽屉界面
    self.leftVC = [[LeftViewController alloc]init];
    self.leftVC.view.frame = CGRectMake(-150 * WIDTHC, 0, 150 *WIDTHC, self.view.frame.size.height - 100);
    
    [self addChildViewController:self.leftVC];
    [self.view addSubview:self.leftVC.view];
    
    //登陆
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    button.frame = CGRectMake(WIDTH * 0.35 , HEIGHT - 100, 100 *WIDTHC , 30 *WIDTHC);
//    [button addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
//    self.loginButton = button;
//    self.loginButton.dk_backgroundColorPicker = DKColorPickerWithRGB(0x1a7a5c,0x175441);
//  //  [self.loginButton setTitle:@"立即登录" forState:UIControlStateNormal];
//    
//    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.imageButton.frame = CGRectMake(WIDTH * 0.38, HEIGHT - 180, 80 * WIDTHC, 80 );
//    self.imageButton.layer.masksToBounds = YES;
//    self.imageButton.layer.cornerRadius = 40 * WIDTHC;
//    self.imageButton.backgroundColor = [UIColor clearColor];
//    [self.view addSubview:self.imageButton];
    
    
    //夜间模式
    UIButton *nightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nightButton.frame = CGRectMake(WIDTH - 70, 10 * WIDTHC, 60 * WIDTHC , 60 * WIDTHC);
    [nightButton setImage:[UIImage imageNamed:@"night"] forState:UIControlStateNormal];
    [self.view addSubview:nightButton];
    
    [nightButton addTarget:self action:@selector(nightButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
    //先默认菜单是关闭的
    self.isDisplyMenu = NO;
    
    
    
    
    // Do any additional setup after loading the view.
}

//夜间模式
-(void)nightButtonAction:(UIButton *)button{
    
    
    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        [self.dk_manager dawnComing];
        NSLog(@"黑天");
        [button setImage:[UIImage imageNamed:@"night"] forState:UIControlStateNormal];
        self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine"]];
        
    }else{
        [self.dk_manager nightFalling];
        NSLog(@"白天");
        [button setImage:[UIImage imageNamed:@"day"] forState:UIControlStateNormal];
        self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mineNight"]];
 }

 
    
}


-(void)loginAction:(UIButton *)button{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 
    NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginStatus"];
    
    if ([login isEqualToString:@"0"]) {
       
        
        //登录第二步
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {//登录成功
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                //       NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//                
//                [defaults setObject:snsAccount.userName forKey:@"userName"];
//                [defaults setObject:snsAccount.iconURL forKey:@"headUrl"];
//                [defaults synchronize];
                
                NSLog(@"%@, %@",snsAccount.userName, snsAccount.iconURL);
                
                
                //登录第三步 ：在登录成功后 获取用户信息
                //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
                [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                    
                    [defaults setObject:snsAccount.userName forKey:@"userName"];
                    [defaults setObject:snsAccount.iconURL forKey:@"headUrl"];
                    [defaults synchronize];

                    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"loginStatus"];
                    
                    [self.loginButton setTitle: snsAccount.userName forState:UIControlStateNormal];
                    [self.imageButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:snsAccount.iconURL]]] forState:UIControlStateNormal];
                    //    NSLog(@"SnsInformation is %@",response.data);
                }];
                
            }
            
        });
     
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            
               [defaults removeObjectForKey:@"userName"];
                [defaults removeObjectForKey:@"headUrl"];
                [defaults setObject:@"0" forKey:@"loginStatus"];
                [defaults synchronize];
            
            [self.loginButton setTitle:@"立即登录" forState:UIControlStateNormal];
            [self.imageButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
       }]];
        
        [self presentViewController:alert animated:YES completion:nil];
 
    }
}


//侧拉菜单按钮
-(void)listButtonAction{
    
    if (self.isDisplyMenu == NO) {
        //显示菜单
        [self show];
        
     //   NSLog(@"222");
    }else{
        [self hidden];
      //  NSLog(@"33");
    }
    self.isDisplyMenu = !self.isDisplyMenu;
    
}



#pragma mark -- 动画展开
-(void)show{
    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.leftVC.view.frame = CGRectMake(0, 0, 150 * WIDTHC, HEIGHT );
      //  self.leftVC.view.backgroundColor = [UIColor whiteColor];
        self.leftVC.view.alpha = 0.5;
        
     //   self.image.frame = CGRectMake(self.leftVC.view.frame.size.width, 0, WIDTH, HEIGHT);
        self.button.frame = CGRectMake(self.leftVC.view.frame.size.width + 10, 10 * WIDTHC, 60 * WIDTHC, 60 * WIDTHC);
         self.imageButton.frame = CGRectMake(WIDTH * 0.38 + self.leftVC.view.frame.size.width, HEIGHT - 180, 80* WIDTHC, 80 * WIDTHC);
        self.loginButton.frame = CGRectMake(WIDTH * 0.35 + self.leftVC.view.frame.size.width, HEIGHT - 100, 100 * WIDTHC, 30 * WIDTHC);
    }];
    
    
    
}

#pragma mark -- 动画收回
-(void)hidden{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.leftVC.view.frame = CGRectMake(-150, 0, 150 * WIDTHC, HEIGHT );
     //   self.leftVC.view.backgroundColor = [UIColor whiteColor];
        self.leftVC.view.alpha = 0.5;
        
      //  self.image.frame = CGRectMake(0, 0,WIDTH, HEIGHT);
        self.button.frame = CGRectMake(10 * WIDTHC, 10 * WIDTHC, 60 * WIDTHC, 60 * WIDTHC);
        
         self.imageButton.frame = CGRectMake(WIDTH * 0.38, HEIGHT - 180, 80 * WIDTHC, 80 * WIDTHC);
        self.loginButton.frame = CGRectMake(WIDTH * 0.35 , HEIGHT - 100, 100 * WIDTHC, 30 * WIDTHC);
    }];
    
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
